/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.Time_frameDAO;
import com.core.database.Manager;
import com.core.entities.Time_frame;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class Time_frameDAOImpl implements Time_frameDAO {

	Connection connection = null;

	@Override
	public Time_frame findByFrom(String from) throws DAOException {
		Time_frame time_frame = new Time_frame();
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM time_frame WHERE from = '" + from + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					time_frame.setId(rs.getInt(1));
					time_frame.setFrom(rs.getString(2));
					time_frame.setTo(rs.getString(3));

					System.out.println(time_frame);
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return time_frame;
	}

	@Override
	public Time_frame findByTo(String to) throws DAOException {
		Time_frame time_frame = new Time_frame();
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM time_frame WHERE to = '" + to + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					time_frame.setId(rs.getInt(1));
					time_frame.setFrom(rs.getString(2));
					time_frame.setTo(rs.getString(3));

					System.out.println(time_frame);
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return time_frame;
	}

	@Override
	public void insert(Time_frame o) throws DAOException {
		Connection connection = null;
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO time_frame (from, to) VALUE (?, ? )";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getFrom());
			ps.setString(2, o.getTo());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Time_frame", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Time_frame findById(int id) throws DAOException {
		Time_frame time_frame = new Time_frame();
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM time_frame WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					time_frame.setId(rs.getInt(1));
					time_frame.setFrom(rs.getString(2));
					time_frame.setTo(rs.getString(3));

					System.out.println(time_frame);
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return time_frame;
	}

	@Override
	public List<Time_frame> findAll() throws DAOException {
		Time_frame time_frame = new Time_frame();
		ArrayList<Time_frame> time_frameList = new ArrayList<Time_frame>();
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM time_frame ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				time_frame.setId(rs.getInt(1));
				time_frame.setFrom(rs.getString(2));
				time_frame.setTo(rs.getString(3));

				time_frameList.add(time_frame);

				System.out.println(time_frame);
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return time_frameList;
	}

	@Override
	public void delete(int id) throws DAOException {
		Time_frame time_frame = new Time_frame();
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM time_frame WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, time_frame.getFrom());
				ps.setString(2, time_frame.getTo());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Time_frame o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE time_frame SET from = '" + o.getFrom()
					+ " ', to = '" + o.getTo() + "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getFrom());
				ps.setString(2, o.getTo());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
